package com.cx.service.impl;

import com.cx.dto.PageInfoResult;
import com.cx.mapper.GoodsMapper;
import com.cx.pojo.Goods;
import com.cx.service.GoodListService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class GoodListServiceImpl implements GoodListService {

    @Autowired
    GoodsMapper goodsMapper;

    @Override
    public List<Goods> selectAll() {
        List<Goods> goodsList = goodsMapper.selectAll();
        return goodsList;
    }

    @Override
    public List<Goods> selectByGoodName(String goodName) {
        List<Goods> goodsList = goodsMapper.selectByGoodName(goodName);
        return goodsList;
    }

    @Override
    public PageInfoResult<Goods> selectAllByPage(PageInfoResult pageParameter) {
        //设置分页参数
        PageHelper.startPage(pageParameter.getCurrPage(),pageParameter.getPageSize());
        List<Goods> goodsList = goodsMapper.selectAll();

        //PageInfo 包装查询结果
        //PageInfo是pageHelper提供的类，用于封装分页查询的数据
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);

        // 查询的当前页数据信息
        List<Goods> empList = pageInfo.getList();
        // 获取查询的总条数  select count(0) from goods
        pageParameter.setTotalSize(pageInfo.getTotal());
        pageParameter.setDataList(empList);

        return pageParameter;
    }

    @Override
    public PageInfoResult<Goods> selectByGoodNameAndPage(PageInfoResult pageParameter, String goodName) {
        //设置分页参数
        PageHelper.startPage(pageParameter.getCurrPage(),pageParameter.getPageSize());
        List<Goods> goodsList = goodsMapper.selectByGoodName(goodName);

        //PageInfo 包装查询结果
        //PageInfo是pageHelper提供的类，用于封装分页查询的数据
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);

        // 查询的当前页数据信息
        List<Goods> empList = pageInfo.getList();
        // 获取查询的总条数  select count(0) from goods
        pageParameter.setTotalSize(pageInfo.getTotal());
        pageParameter.setDataList(empList);

        return pageParameter;
    }

    @Override
    public PageInfoResult<Goods> selectBySelectiveAndPage(PageInfoResult pageParameter, String goodName,Integer type, Integer types, BigDecimal lowPrice, BigDecimal upPrice) {
        //设置分页参数
        PageHelper.startPage(pageParameter.getCurrPage(),pageParameter.getPageSize());
        List<Goods> goodsList = goodsMapper.selectBySelectiveAndPage(goodName,type,types,lowPrice,upPrice);

        //PageInfo 包装查询结果
        //PageInfo是pageHelper提供的类，用于封装分页查询的数据
        PageInfo<Goods> pageInfo = new PageInfo<>(goodsList);

        // 查询的当前页数据信息
        List<Goods> empList = pageInfo.getList();
        // 获取查询的总条数  select count(0) from goods
        pageParameter.setTotalSize(pageInfo.getTotal());
        pageParameter.setDataList(empList);

        return pageParameter;
    }
}
